package com.example.thirdannotation.y2022.annotation.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class DemoUtil {

    private static String name;

    private static String age;

    private static String sex;

    @Value("${user.age}") //静态属性的注入需要通过set方法，不能直接在属性上注解
    public void setAge(String age) {
        System.out.println(2);
        DemoUtil.age = age;
    }
    @Value("${user.name}")
    public void setName(String name) {
        DemoUtil.name = name;
    }
    @Value("${user.sex}")
    public void setSex(String sex) {
        DemoUtil.sex = sex;
    }

    public void setMap(Map<String, String> map) {
        DemoUtil.map = map;
    }

    public static String getSex() {
        return sex;
    }

    public static String getName() {
        return name;
    }

    public static String getAge() {
        return age;
    }

    private static Map<String, String> map;
    DemoUtil(){
        System.out.println("1");
        Map<String, String> map = new HashMap<>();
        System.out.println(getName());
        System.out.println(getSex());
        map.put(getSex(),getAge());
//        System.out.println(JSON.toJSONString(map));
        setMap(map);
    }

    public static Map<String, String> getMap() {
        Map<String, String> map = new HashMap<>();
        map.put(sex,age);
        return map;
    }

    public static String getMyAge(String sex){
        return getMap().get(sex);
    }

}
